package com.gurid.service.edu.controller.admin;

import com.gurid.common.base.result.R;
import com.gurid.service.edu.entity.Video;
import com.gurid.service.edu.service.VideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Gurid
 * @since 2024-04-29
 */
@Tag(name = "VideoController", description = "课时管理")
@RestController
//@CrossOrigin
@RequestMapping("/admin/edu/video")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Operation(description = "新增课时")
    @PostMapping("save")
    public R save(
            @Parameter(description = "课时对象", required = true)
            @RequestBody Video video){

        boolean result = videoService.save(video);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @Operation(description = "根据ID查询课时")
    @GetMapping("get/{id}")
    public R getById(
            @Parameter(description = "课时id", required = true)
            @PathVariable String id){

        Video video = videoService.getById(id);
        if (video != null) {
            return R.ok().data("item", video);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @Operation(description = "更新课时")
    @PutMapping("update")
    public R updateCourseInfoById(
            @Parameter(description = "课时基本信息", required = true)
            @RequestBody Video video){

        boolean result = videoService.updateById(video);
        if (result) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }

    @Operation(description = "根据id删除课时")
    @DeleteMapping("remove/{id}")
    public R removeById(
            @Parameter(description = "课时id", required = true)
            @PathVariable String id){

        //OSS层删除课时视频
        videoService.removeMediaVideoById(id);

        boolean result = videoService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }
}
